package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysRoleDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.params.SysRoleParams;
import com.ruoyi.system.mapper.SysRoleDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 角色 业务层处理
 *
 * @author ruoyi
 */
@Service
@Slf4j
public class SysRoleDeptServiceImpl implements ISysRoleDeptService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private SysRoleDeptMapper roleDeptMapper;


    @Override
    public List<SysRoleDept> selectRoleDeptList(SysRoleDept roleDept) {
        return roleDeptMapper.selectRoleDeptList(roleDept);
    }

    @Override
    public Set<Long> selectRoleBottomDept(Long deptId, String roleKey) {
        List<Long> deptIds = sysDeptService.selectDeptAncestors(deptId);
        deptIds.add(deptId);
        Set<Long> sysRoleDeptSet = getRoleDept(deptIds, roleKey, true);
        return sysRoleDeptSet;
    }

    @Override
    public List<SysRoleDept> getDiffRoleDept(Long deptId, String roleKey, Long[] menuIds) {
        List<Long> menuIdList = new ArrayList<>();
        for (Long menuId : menuIds) {
            menuIdList.add(menuId);
        }
        return getDiffRoleDept(deptId, roleKey, menuIdList);
    }

    /**
     * 获取包含自己的角色菜单
     *
     * @param deptId
     * @param roleKey
     * @return
     */
    @Override
    public Set<Long> getRoleDeptContainSelf(Long deptId, String roleKey) {
        List<Long> deptIds = sysDeptService.selectDeptAncestors(deptId);
        deptIds.add(deptId);
        return getRoleDept(deptIds, roleKey);
    }

    /**
     * 获取排除自己的角色菜单
     *
     * @param deptId
     * @param roleKey
     * @return
     */
    @Override
    public Set<Long> getRoleDeptExcludeSelf(Long deptId, String roleKey) {
        List<Long> deptIds = sysDeptService.selectDeptAncestors(deptId);
        return getRoleDept(deptIds, roleKey);
    }

    @Override
    public Set<Long> getRoleDept(List<Long> deptIds, String roleKey) {
        return getRoleDept(deptIds, roleKey, false);
    }

    /**
     * @param deptIds
     * @param roleKey
     * @param isOnlyBottomDept 是否获取仅仅底层的菜单
     * @return
     */
    @Override
    public Set<Long> getRoleDept(List<Long> deptIds, String roleKey, Boolean isOnlyBottomDept) {
        isOnlyBottomDept = isOnlyBottomDept == null ? false : isOnlyBottomDept;
        Set<Long> sysRoleDeptSet = new HashSet<>();
        if (deptIds.size() > 0) {
            SysRoleParams sysRoleParams = new SysRoleParams();
            sysRoleParams.setDeptIds(deptIds);
            sysRoleParams.setRoleKey(roleKey);
//            sysRoleParams.setDelFlag("0");
//            sysRoleParams.setStatus("0");
//            sysRoleParams.setStatus("0");
            Page<SysRole> page = new Page<>(1, -1);
            List<SysRole> sysRoleList = roleMapper.selectRoleList(page,sysRoleParams).getRecords();
            Map<String, SysRole> sysRoleMap = new HashMap<>();
            for (SysRole sysRole : sysRoleList) {
                //需要添加删除状态作为唯一标识 否则可能会拿到删除的数据  后设计数据只有一条 如果存在删除的就激活
                sysRoleMap.put(sysRole.getDeptId() + "_" + sysRole.getRoleKey(), sysRole);
            }
//            Map<Long,SysRoleDept> sysRoleDeptMap = new HashMap<>();
            for (Long deptIdTemp : deptIds) {
                SysRole sysRole = sysRoleMap.get(deptIdTemp + "_" + roleKey);
                if (sysRole != null) {
                    //判断当没有删除并且状态没有停用并且允许继承的
                    if (!"2".equals(sysRole.getDelFlag()) &&
                            !"1".equals(sysRole.getStatus()) &&
                            !"N".equals(sysRole.getIsAllowExtends())) {
//                    roleIds.add(sysRole.getRoleId());
                        SysRoleDept roleDeptParam = new SysRoleDept();
                        roleDeptParam.setRoleId(sysRole.getRoleId());
                        List<SysRoleDept> roleDeptList;
//                        if(isOnlyBottomDept){
                        roleDeptList = roleDeptMapper.selectRoleDeptList(roleDeptParam);
/*                        }else{
                            roleDeptList = roleDeptMapper.selectRoleDeptBottomList(roleDeptParam);
                        }*/
                        for (SysRoleDept sysRoleDeptTemp : roleDeptList) {
                            //判断是否为例外当不是例外的时候 添加 当为例外的是删除
                            if (sysRoleDeptTemp.getIsException().equals("N")) {
                                sysRoleDeptSet.add(sysRoleDeptTemp.getDeptId());
                            } else {
                                sysRoleDeptSet.remove(sysRoleDeptTemp.getDeptId());
                            }
                        }
                    }
                }
            }
        }

        if (isOnlyBottomDept) {
            sysRoleDeptSet = sysDeptService.rejectParentDept(sysRoleDeptSet);
        }
        //
        return sysRoleDeptSet;
    }

    @Override
    public List<SysRoleDept> getDiffRoleDept(Long deptId, String roleKey, List<Long> deptIdList) {
        //获取所有父级别的菜单
        Set<Long> sysRoleDeptSet = getRoleDeptExcludeSelf(deptId, roleKey);

        //sysRoleDeptSet 与 deptIds进行对比
        //为了不影响原始数据
        List<Long> deptIdListTemp = new ArrayList<>();
        deptIdListTemp.addAll(deptIdList);

        // 新增用户与角色管理

        for (int i = 0; i < deptIdListTemp.size(); i++) {
            Long deptTempId = deptIdListTemp.get(i);
            //当存在的时候相互抵消
            if (sysRoleDeptSet.contains(deptTempId)) {
                deptIdListTemp.remove(i);
                sysRoleDeptSet.remove(deptTempId);
                i--;
            }
        }
        List<SysRoleDept> list = new ArrayList<SysRoleDept>();
        //当子级存在而父级 加入为非例外
        for (int i = 0; i < deptIdListTemp.size(); i++) {
            Long deptTempId = deptIdListTemp.get(i);
            SysRoleDept sysRoleDept = new SysRoleDept();
            sysRoleDept.setDeptId(deptTempId);
            sysRoleDept.setIsException("N");
            list.add(sysRoleDept);
        }

        //当父级存在而子级不存在的时候 加入列外
        for (Long deptTempId : sysRoleDeptSet) {
            SysRoleDept sysRoleDept = new SysRoleDept();
            sysRoleDept.setDeptId(deptTempId);
            sysRoleDept.setIsException("Y");
            list.add(sysRoleDept);
        }
        return list;
    }
}
